﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Frame.Lib.Core.Base;
using Cms.Entity.Traffic;
using Cms.DAL.Traffic;
using Frame.Lib.Web;
using Frame.Lib.Core.Company;


namespace Cms.BLL.Traffic
{
    public class IPTraficBO : CommonOperate<IPTraficInfo>
    {
        private IPTraficDAL _ip;
        protected override BaseDAL DAL
        {
            get
            {
                return _ip;
            }
        }
        public IPTraficBO()
        {
            _ip = new IPTraficDAL();
        }

        public IEnumerable<IPTraficInfo> GetList(PagerSortSetting setting, string startTime, string endTime)
        {
            string condition = " 1=1 ";
            if (!string.IsNullOrEmpty(startTime))
            {
                condition += " and InputTime>=@p0 ";
            }
            if (!string.IsNullOrEmpty(endTime))
            {
                condition += " and InputTime <= @p1";
            }
            IEnumerable<IPTraficInfo> list = base.GetList(setting, condition, startTime, endTime);
            return list;
        }

        public void Delete()
        {
            _ip.Delete();
        }

        public void Delete(string startTime, string endTime)
        {
            _ip.Delete(startTime, endTime);
        }

        public void Save()
        {
            System.Web.HttpRequest request = System.Web.HttpContext.Current.Request;

            IPTraficInfo info = new IPTraficInfo();
            info.IP = request.UserHostAddress;

            DateTime begin = DateTime.Now.AddDays(-1);
            DateTime end = DateTime.Now;

            if (!_ip.Exist(info.IP, begin, end))
            {
                info.ID = Guid.NewGuid();
                info.InputTime = DateTime.Now;
                base.Save(info, true);

                CompanyBO company = new CompanyBO();
                CompanyInfo comInfo = company.GetInfo();
            
                if (comInfo != null)
                {
                   
                    comInfo.SiteIPCount++;

                    company.Save(comInfo);
                }
            }
        }
    }
}
